package com.aerozhonghuan.logisticsmall.modules.order;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarActivity;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.order.adapter.MyOrdersViewPagerAdapter;
import com.aerozhonghuan.logisticsmall.modules.order.logic.OrderStateControl;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述 我的订单页面
 * @author zhangyonghui
 * 创建日期 2019-05-24
 */
public class MyOrdersAcrivity extends TitlebarActivity {

    public static final String KEY_POSITION = "KEY_POSITION";
    private FrameLayout container;
    private LayoutInflater inflater;
    private View view;
    private TabLayout tablayout;
    private ViewPager viewpager;
    private String[] tabNames;
    private List<TitlebarFragment> orderFragments;
    private MyOrdersViewPagerAdapter myOrdersViewPagerAdapter;
    private int position;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState == null) {
            container = (FrameLayout) findViewById(R.id.container1);
            inflater = LayoutInflater.from(getContext());
            view = inflater.inflate(R.layout.activity_myorders, null);
            container.addView(view);
            position = getIntent().getIntExtra(KEY_POSITION, -1);
            initView();
        }
    }

    private void initView() {
        viewpager = (ViewPager) view.findViewById(R.id.viewpager);
        // 初始化设置tablayout
        tablayout = (TabLayout) view.findViewById(R.id.tablayout);
        tabNames = new String[OrderStateControl.orderStates.size() + 1];
        orderFragments = new ArrayList<>();
        tabNames[0] = "全部";
        for (int i = 0; i < tabNames.length; i++) {
            tablayout.addTab(tablayout.newTab());
            Bundle bundle = new Bundle();
            if (i > 0) {
                tabNames[i] = OrderStateControl.orderStates.get(i - 1).name;
                bundle.putInt(MyOrdersFragment.KEY_STATUS, OrderStateControl.orderStates.get(i - 1).status);
            }
            MyOrdersFragment myOrdersFragment = new MyOrdersFragment();
            myOrdersFragment.setArguments(bundle);
            orderFragments.add(myOrdersFragment);
            tablayout.getTabAt(i).setText(tabNames[i]);
        }
        myOrdersViewPagerAdapter = new MyOrdersViewPagerAdapter(this, orderFragments, tabNames);
        viewpager.setAdapter(myOrdersViewPagerAdapter);
        tablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tablayout.setupWithViewPager(viewpager);
        tablayout.getTabAt(position + 1).select();
    }
}
